﻿var hss = null;

// Called from Interlink to initialize the Javascript environment.
function InitializeInterlink()
{
	try
	{

		// Capture SL Control
		hss = document.getElementById("hssCtl");

		// ****************************************
		// Events
		// ****************************************

		// Occurs when the state of the Upload changes.
		hss.Content.Interlink.StateChanged = OnStateChanged;

		// Occurs when the progress percentage changes.
		hss.Content.Interlink.UploadProgressChanged = OnUploadProgressChanged;

		// Occurs when the upload completes.
		hss.Content.Interlink.UploadCompleted = OnUploadCompleted;

		// Occurs when a single file is added.
		hss.Content.Interlink.FileAdded = GenerateFileList;

		// Occurs when a single file is removed.
		hss.Content.Interlink.FileRemoved = GenerateFileList;

		// Occurs when a single file is changed.
		hss.Content.Interlink.FileChanged = GenerateFileList;

		// ****************************************
		// End Events
		// ****************************************

		// ****************************************
		// Configuration - All values below are their default values.
		// ****************************************

		// Gets or sets whether or not to overwrite existing files.
		//hss.Content.Interlink.AllowFileOverwrite = true;

		// Gets or sets whether or not the user can select multiple files.
		//hss.Content.Interlink.AllowMultiSelect = true;

		// Gets or sets whether or not once the first upload process completes, if the User can initiate a new upload.
		//hss.Content.Interlink.AllowNewUpload = true;

		// Gets or sets whether or not to begin uploading immediately after file selection.
		//hss.Content.Interlink.AutoUpload = false;

		// Gets or sets the configuration string to provide to the configurated IUploadClient client during intialization.
		// For Azure we set this value based on the results of our JSON Get Client Config call (see call to retrieve the azure config - below)
		//hss.Content.Interlink.ClientConfig = "UploadHandler"; // This default value is the app setting key to your server side file handler

		// Gets or sets a string defining the Type of the <see cref="IUploadClient"/> that will process the upload for a single file.
		//hss.Content.Interlink.ClientType = "HSS.Interlink.UploadClient";
		hss.Content.Interlink.ClientType = "HSS.Interlink.UploadClientAzure"; // For Windows Azure

		// Gets or sets a comment for display.
		//hss.Content.Interlink.Comment = "Select one or more files to be uploaded.";

		// Gets or sets the pipe delimited list of file extensions that are prohibited. Example: .exe|.mdb
		//hss.Content.Interlink.ExcludeFileExtension = ".exe|.mdb|.mde|.accdb|.accde";

		// Gets or sets a pipe delimeted string of allowed file extensions that can be uploaded.
		//hss.Content.Interlink.Filters = "All Files (*.*)|*.*";

		// Gets or sets the maximum number of concurrent files that can be uploaded at a time.
		//hss.Content.Interlink.MaximumConcurrency = "2";
		
		// Gets or sets the maximum size, in bytes, of any one file that can be uploaded.
		//hss.Content.Interlink.MaximumFileSize = "2147483647";

		// Gets or sets the text to be displayed when there are no files to upload.
		//hss.Content.Interlink.NoFilesText = "* Click the Browse button to select one or more files\r\n* Rightclick and click Browse to select one or more files\r\n* Drag the file(s) you want to upload into this area.";

		// Gets or sets the uri of a remote service to receive the file(s).
		// For Azure we set this value based on the results of our JSON Get Client Config call (see call to retrieve the azure config - below)
		//hss.Content.Interlink.ServiceUri = "/FileUpload.ashx";

		// Gets or sets a title for display.
		//hss.Content.Interlink.Title = "Upload Files";
		hss.Content.Interlink.Title = "Windows Azure";

		// Gets or sets the Uri of a DataTemplate resource to load that supports the Type defined in the UploadFileType property.
		//hss.Content.Interlink.UploadFileDataTemplate = "/HSS.Interlink.UI;component/uploadfile.xaml";

		// Gets or sets the fully qualified name of the Type of the model that should contain an individual file for upload.
		//hss.Content.Interlink.UploadFileType = "HSS.Interlink.UploadFile";

		// Called to retrieve the azure config.
		$.post('/home/initazureconfig', null,
		function (data)
		{
			hss.Content.Interlink.ServiceUri = data.serviceUri;
			hss.Content.Interlink.ClientConfig = data.clientConfig;
		});

		// ****************************************
		// End Configuration
		// ****************************************

	}
	catch (e)
	{
		alert(e);
	}
}
// Called from Interlink to collect optional metadata to send with a single file.
// Is called for each file that is uploaded.
function GetMetadata(fileName)
{
	try
	{
		// Azure requires a key-value pair.
		return "x-ms-meta-comment=Processed from Javascript!";
		//return fileName + " processed from Javascript!";
	}
	catch (e)
	{
		alert(e);
	}
}

// Event Handlers
function OnStateChanged(s, e)
{
	try
	{
		document.getElementById('UploadState').innerHTML = hss.Content.Interlink.StateFormatted;
	}
	catch (e)
	{
		alert(e);
	}
}
function OnUploadProgressChanged(s, e)
{
	SetProgress(e.ProgressPercentage);
}
function OnUploadCompleted(s, e)
{
	try
	{
		document.getElementById('UploadState').innerHTML = hss.Content.Interlink.StateFormatted;
	}
	catch (e)
	{
		alert(e);
	}
}

// Helper Methods
function GenerateFileList()
{
	var list = "<table><tr><td><b>File Name</b></td><td><b>State</b></td><td><b>Percentage</b></td></tr>";
	var userFile;

	var i = 0;
	for (i = 0; i < hss.Content.Interlink.FileList.length; i++)
	{
		userFile = hss.Content.Interlink.FileList[i];
		list += "<tr><td>" + userFile.FileName + "&nbsp;&nbsp;</td><td>" + userFile.StateString + "</td><td>" + userFile.Percentage + "</td></tr>";
	}

	list += "</table>"

	document.getElementById('FileList').innerHTML = list;

	UpdateTotal();
	UpdateUploaded();
}
function UpdateTotal()
{
	document.getElementById('TotalSelected').innerHTML = hss.Content.Interlink.TotalCount;
}
function UpdatePercentage()
{
	SetProgress(hss.Content.Interlink.Percentage);
}
function UpdateUploaded()
{
	document.getElementById('TotalUploaded').innerHTML = hss.Content.Interlink.FinishedCount;
}
function SetProgress(newvalue)
{
	document.getElementById('TotalPercentage').innerHTML = newvalue + "%";
}

// Actions
function UploadAsync()
{
	if (hss != null)
	{
		hss.Content.Interlink.UploadAsync();
	}
}
function Reset()
{
	if (hss != null)
	{
		hss.Content.Interlink.Reset();
	}
}
function RemoveAt(index)
{
	if (hss != null)
	{
		hss.Content.Interlink.RemoveAt(index);
	}
}
function OnlyJPEGFiles()
{
	hss.Content.Interlink.Filters = "Images (*.jpg) | *.jpg";
}

// Alerts
function AlertUploadFiles()
{
	if (hss != null)
	{
		alert("Uploaded Files: " + hss.Content.Interlink.FinishedCount);
	}
}
function AlertTotalFiles()
{
	if (hss != null)
	{
		alert("Total Files: " + hss.Content.Interlink.TotalCount);
	}
}
function AlertProgressPercentage()
{
	if (hss != null)
	{
		alert("Progress: " + hss.Content.Interlink.Percentage + "%");
	}
}